<?php

namespace App\Admin\Forms;

use App\Models\UserTitle;
use Dcat\Admin\Widgets\Form;

class AdminSetting extends Form
{
    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {

        admin_setting($input);
        return $this
            ->response()
            ->success('保存成功')
            ->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->tab('签到配置',function (Form $form){
            $form->number('sign_give_points','签到赠送积分')
                ->min(0)
                ->value(admin_setting('sign_give_points'));

            $form->table('sign_give_rules','连签规则',function ($table) {
                $table->number('sign_day','签到天数')->required();
                $table->number('give_points','额外赠送积分')->required();
            })->value(admin_setting('sign_give_rules'));


        })->tab('发布配置',function (Form $form){
            $form->radio('allow_publish','发布开关')
                ->options([1 => '开启',2 => '关闭'])
                 ->default(1)
                 ->value(admin_setting('allow_publish'));

        })->tab('广告配置',function (Form $form){

            $form->editor('ad_put_agreement','投放协议')
                 ->value(admin_setting('ad_put_agreement'));
        })->tab('用户协议',function (){
            $this->editor('user_protocol','用户协议')
                 ->value(admin_setting('user_protocol'));
        })->tab('关于我们',function (){
            $this->editor('about_me','关于我们')->disk('oss')->value(admin_setting('about_me'));
        })->tab('平台客服',function (Form $form){
            $form->embeds('customer_service_number', '电话',function ($form) {
                $form->text('ad_customer_service_number','广告投放电话');
                $form->text('sale_customer_service_number','售后服务电话');
                $form->text('complain_customer_service_number','投诉建议电话');
                $form->text('store_customer_service_number','开店服务电话');
                $form->text('develop_customer_service_number','发展建议电话');

            })->value(admin_setting('customer_service_number'));

        })->tab('APP配置',function (Form $form){
            $form->embeds('app_info_array', 'APP信息',function ($form) {
                $form->file('app_url','安装包')
                    ->autoUpload()
                    ->uniqueName()
                    //->disk('oss')
                    ->maxSize(1024*100)
                    ->chunked()
                    ->removable(false)
                    ->saveFullUrl();

                $form->text('app_version','版本号')
                    ->help('新版版本号必须大于旧版本号');
            })->value(admin_setting('app_info_array'));
        })->tab('分润配置',function (Form $form){
            $form->embeds('send_gift_share_profit', '打赏分润',function ($form) {
                $form->rate('platform_rate','平台抽点比例');
                $form->rate('direct_distribution_rate','直推分润比例');
                $form->rate('interposition_rate','间推分润比例');
            })->value(admin_setting('send_gift_share_profit'));

            $form->embeds('course_share_profit', '课程分润',function ($form) {
                $form->rate('platform_rate','平台抽点');
                $form->rate('direct_distribution_rate','直推分润比例');
                $form->rate('interposition_rate','间推分润比例');
            })->value(admin_setting('course_share_profit'));


            $form->embeds('ad_share_profit', '广告分润',function ($form) {
                $form->rate('platform_rate','平台抽点');
                $form->rate('direct_distribution_rate','直推分润比例');
                $form->rate('interposition_rate','间推分润比例');
            })->value(admin_setting('ad_share_profit'));
        })->tab('热门品牌',function (Form $form){

            $form->embeds('hot_brand',function ($form){
                $form->image('popularity_brand','人气品牌')
                    ->disk('oss')
                    ->autoUpload()
                    ->removable(false)
                    ->uniqueName()
                    ->saveFullUrl();

                $form->image('hot_list_brand','热榜品牌')
                    ->disk('oss')
                    ->autoUpload()
                    ->removable(false)
                    ->uniqueName()
                    ->saveFullUrl();

                $form->image('winnow_brand','精选品牌')
                    ->disk('oss')
                    ->autoUpload()
                    ->removable(false)
                    ->uniqueName()
                    ->saveFullUrl();
                $form->image('internation_brand','国际品牌')
                    ->disk('oss')
                    ->autoUpload()
                    ->removable(false)
                    ->uniqueName()
                    ->saveFullUrl();

            })->value(admin_setting('hot_brand'));


        })->tab('免费权益',function (Form $form){

            $form->number('consult_count','咨询次数')
                ->value(admin_setting('consult_count'))
                ->min(0)
                 ->required();
            $form->number('ad_order_count','广告接单次数')
                ->value(admin_setting('ad_order_count'))
                ->min(0)
                 ->required();
            $form->number('circle_publish_count','发布圈子次数')
                ->min(0)
                ->value(admin_setting('circle_publish_count'))
                ->required();

            $form->select('user_title_id','可查看内容')
                ->options(UserTitle::getOption())
                ->value(admin_setting('user_title_id'))
                ->saving(function ($value){
                    return (int)$value;
                })->help('不选择将可查看全平台内容');

        });
    }

}
